package BanyuanClub.Work.Story.Four;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BankServer {
    private FAccount[] fAccounts = new FAccount[5];
    //所有账户
    {
        for (int i = 1; i <= fAccounts.length; i++) {
            fAccounts[i-1] = new FAccount("1000"+i,"5555"+i,1*2000);
        }
    }

    //校验卡号
    public String typeInTheCode(String inCode) {
        String myCode = myCard(inCode);
        if (!(myCode == null))
            return myCode;
        return null;
    }

    //校验密码
    public String typeInThePassword(String inCode, String passwords){
        FAccount myAccount = myAccound(inCode);

            if (myAccount.getPassword().equals(passwords)){
                return passwords;
            }

        return null;
    }

    //操作指令
    //存款
    public String inMoney (int money,String inCode){
        int s = typeInMoney(money);
        FAccount myAccount = myAccound(inCode);
        if (s == money){
            myAccount.setBalance(myAccount.getBalance()+money);
            return  "存款成功";
        }
        return null;
    }

    //取款
    public String outMoney(int money, String inCode){
        int s = typeInMoney(money);
        FAccount myAccount = myAccound(inCode);
         if (money == s){
             if (s > myAccount.getBalance()){
                 return "余额不足";
             }else {
            myAccount.setBalance(myAccount.getBalance() - money);
            return  "请收好你的钱";
             }
        }
        return null;
    }

    //查询
    public void query(String inCode){
        FAccount myAccount = myAccound(inCode);
        System.out.println("您的余额为"+myAccount.getBalance());
    }

    //转账
    public String yourCode(String inYourCode,int money,String inCodePassword,String inCode){
        FAccount myAccounts = myAccound(inCode);
        String yourCode = myCard(inYourCode);
        if (yourCode == null)
            return "对方账户不存在";
        if (money > myAccounts.getBalance())
            return "余额不足";
        if (!inCodePassword.equals(myAccounts.getPassword()))
            return "密码不正确";
        //开始转账
        FAccount yourAccount = myAccound(inYourCode);
        myAccounts.setBalance(myAccounts.getBalance()-money);
        yourAccount.setBalance(yourAccount.getBalance()+money);
        return "转账成功";
    }

    //查找卡号
    public String myCard(String inCode){
        for (int i = 0; i < fAccounts.length; i++) {
            if (inCode.equals(fAccounts[i].getIdCard())) {
                return fAccounts[i].getIdCard();
            }
        }
        return null;
    }

    //查找指定账户
    public FAccount myAccound(String inCode){
        for (int i = 0; i < fAccounts.length; i++) {
            if (inCode.equals(fAccounts[i].getIdCard())) {
                return fAccounts[i];
            }
        }
        return null;
    }

    //钱为整百且不超过5000
    public int typeInMoney(int money){
        int[] arr = new int[50];
        for (int i = 1; i <= arr.length; i++) {
            arr[i-1] = i*100;
        }
        for (int i = 0; i < arr.length; i++) {
            if (money == arr[i]){
                return money;
            }
        }
        return 12;
    }
}
